Vue.component("form_com", {
	props: ['parent_data'],
	template: '#oneQuestion-template',
	data: function () {
		console.log("children data");
		return {
			//value: this.getValue(this.parent_data.current_question.ControlType),
			selectItem: null
		}
	},
	computed: {
		isStop: function () {
			if (this.selectItem) {
				var IsLast = this.selectItem.IsLast;
				if (IsLast) {
					return true;
				}
			}
			return false;
		},
		hasTip: function () {
			if (this.selectItem) {
				var msg = this.selectItem.ShowMsg;
				if (msg) {
					this.parent_data.component_data.ShowMsg = msg;
					return msg.length > 0;
				}
			}
			return false;
		}
	},
	mounted: function () {
		console.log("children mounted");
	},
	watch: {
		// value: function (val, oldval) {
		// 	this.parent_data.component_data.selectedValue = val;
		// }
	},
	methods: {
		getValue: function (type) {
			var v = this.parent_data.component_data.selectedValue;
			if (v) {
				return v;
			}
			if (type == 4) {
				return [];
			} else {
				return "";
			}
		},
		change_radio: function (AnswerItem) {
			this.selectItem = AnswerItem;
			this.parent_data.component_data.isStop = this.isStop;
			this.parent_data.component_data.hasTip = this.hasTip;
		}
	}

})


//离岸信托控件
Vue.component('asset_control', {
	template: '	<div><template v-for="(item1,index) in parent_value">\
				<div class="form-group" v-if="item1.Level==1">\
					<label for="exampleInputEmail1">{{item1.CategoryName}}</label>\
				</div>\
				<div class="form-group" v-for="(item2,index2) in parent_value" v-if="item2.ParentID==item1.ID">\
					<template v-if="control_type==9">\
						<label for="exampleInputEmail1">多选：{{item2.CategoryName}}</label>\
						<div>\
							<label class="checkbox-inline">\
								<input type="checkbox" v-model="checkbox_values" v-bind:id="item2.ID" v-bind:value="item2.ID+\'|A\'"> {{item2.LastTitle1}}\
							</label>\
							<label class="checkbox-inline">\
								<input type="checkbox" v-model="checkbox_values" v-bind:id="item2.ID"  v-bind:value="item2.ID+\'|B\'"> {{item2.LastTitle2}}\
							</label>\
						</div>\
					</template>\
					<template v-if="control_type==10">\
						<label for="exampleInputEmail1">单选：{{item2.CategoryName}}</label>\
						<div>\
							<label class="checkbox-inline">\
								<input type="radio" v-model="radio_values[index2]" v-bind:value="item2.ID+\'|A\'" > {{item2.LastTitle1}}\
							</label>\
							<label class="checkbox-inline">\
								<input type="radio" v-model="radio_values[index2]" v-bind:value="item2.ID+\'|B\'" > {{item2.LastTitle2}}\
							</label>\
						</div>\
					</template>\
				</div>\
			</template></div>',
	data: function () {
		return {
			selectedIds: "",
			checkbox_values: this.set_checkbox_data(),
			radio_values: this.set_radio_data()
		}
	},
	props: ['parent_value', "control_type", "control_out"],
	watch: {
		checkbox_values: function (val, oldval) {
			this.updateValue(val);
		}, radio_values: function (val, oldval) {
			this.updateValue(val);
		}
	},
	methods: {
		set_checkbox_data: function () {
			var self = this;
			if (self.control_type == 9) {//多选
				var groupAnswers = self.control_out.all_values.split(';');
				var vals = [];
				$(groupAnswers).each(function (i, item) {
					if (item) {
						var nv = item.replace(',', '|');
						vals.push(nv);
					}
				});

				return vals;
			}
			return [];
		},
		set_radio_data: function () {
			if (this.control_type == 10) {//单选
				return this.convert_answers_to_options();
			}
			return [];
		},
		convert_answers_to_options: function () {
			var self = this;
			var groupAnswers = self.control_out.all_values.split(';');
			var vals = [];
			//遍历所有题，赋初值
			$(self.parent_value).each(function (i, item1) {
				if (item1.Level == 1) {
					$(self.parent_value).each(function (j, item2) {
						if (item1.ID == item2.ParentID) {
							vals.push(item2.ID);
						} else {
							vals.push("");
						}
					})
				}
			});

			//对应每个选项，给出对应答案，如果没有，则为空
			for (var index = vals.length; index > 0; index--) {
				var matchedValue = "";
				var id = vals[index - 1];
				if (id) {
					$(groupAnswers).each(function (i, item) {
						if (item) {
							var nv = item.replace(',', '|');
							if (nv.indexOf(id) > -1) {
								matchedValue = nv;
								return false;
							}
						}
					});
				} 
				vals[index - 1] = matchedValue;
			}
			return vals;
		},
		updateValue: function (val) {
			var self = this;
			//题目规则
			var value = 0;
			self.selectedIds = "";
			$(val).each(function (index, item) {
				if (item.indexOf("|") > -1) {
					self.selectedIds += item.split('|')[0] + ",";
					var v = item.split('|')[1];
					if (!value)
						value = v;
					if (v == "B")
						value = v;
				}
			})

			self.control_out.err = "";
			self.control_out.result = value;
			self.control_out.all_values = self.removeEmpty(val);
			var msg = this.checkRequired();
			if (msg) {
				//进行赋值
				self.control_out.err = msg;
				self.control_out.result = "";
				self.control_out.all_values = "";
			}
		},
		removeEmpty: function (v) {
			var newVue = "";
			$(v).each(function (i, item) {
				if (item.indexOf("|") > -1) {
					newVue += item.replace('|', ',') + ";"
				}
			});
			return newVue;
		},
		checkRequired: function () {
			var self = this;
			var isSelected = false;
			var msg = "";
			var requiredIds = self.getRequiredIds();
			$(requiredIds).each(function (i, item) {

				isSelected = false;
				$(item.Ids).each(function (si, id) {
					if (self.selectedIds.indexOf(id) == -1) {
						isSelected = true;
						return false;
					}
				});

				if (isSelected) {
					msg = item.CategoryName;
					return false;
				}
			})

			if (isSelected) {
				return msg;
			}
			return "";
		},
		getRequiredIds: function () {
			var self = this;
			var requiredTypeIds = [];
			$(self.parent_value).each(function (i, item) {
				if (item.IsRequired) {
					requiredTypeIds.push(item);
				}
			});

			var requiredIds = [];
			$(requiredTypeIds).each(function (pIndex, pItem) {
				$(self.parent_value).each(function (i, item) {
					if (item.ParentID == pItem.ID) {
						var existPId = self.getExistPId(requiredIds, pItem.ID);
						if (!existPId) {
							existPId = {};
							existPId.ParentID = pItem.ID;
							existPId.CategoryName = pItem.CategoryName;
							existPId.Ids = [];
							requiredIds.push(existPId);
						}
						existPId.Ids.push(item.ID);
					}
				});
			});
			return requiredIds;
		},
		getExistPId: function (rIds, pid) {
			var bExist = null;
			$(rIds).each(function (i, item) {
				if (item.ParentID == pid) {
					bExist = item;
					return false;
				}
			});
			return bExist;
		}
	}
})

